﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Handrise.CDCable.Mobile.Business;

namespace Handrise.CDCable.Mobile.UI
{
    public partial class FrmCommunication : FrmBase
    {
        private CommunicationService communicationService = new CommunicationService();

        public FrmCommunication()
        {
            InitializeComponent();
            ttlbrhead.Text = "指挥与反馈";
            ilstbcup.Images.Add(ResourceImage.发送01);
            ilstbcup.Images.Add(ResourceImage.键盘01);
            ilstbcup.Images.Add(ResourceImage.返回01);
            ilstdown.Images.Add(ResourceImage.发送02);
            ilstdown.Images.Add(ResourceImage.键盘02);
            ilstdown.Images.Add(ResourceImage.返回02);


        }

        private void FrmCommunication_Load(object sender, EventArgs e)
        {
            LoadData();

            communicationService.OnDownloadCommunicationComplete += new DownloadCommunicationCompleteHandler(communicationService_OnDownloadCommunicationComplete);

            GetDatatimer.Enabled = true;
            GetDatatimer_Tick(null,null);
        }

        /// <summary>
        /// 接收到新的指挥信息
        /// </summary>
        /// <param name="communications"></param>
        private void communicationService_OnDownloadCommunicationComplete(List<Object> communications)
        {
            foreach (Communication communication in communications)
            {
                if (tbAll.Text != "")
                    tbAll.Text += "\r\n\r\n" + communication.RecordDateTime.ToString() + "  " + communication.Sender + "：";
                else
                    tbAll.Text += communication.RecordDateTime.ToString() + "  " + communication.Sender + "：";

                tbAll.Text += "\r\n" + communication.Content;
            }

            GotoLast(tbAll);
        }

        private void LoadData()
        {
            List<Communication> communications = communicationService.GetCommunicationsByDateTime(AppLogic.LoginUser.UserName, DateTime.Now.AddMonths(-1), DateTime.Now);
            foreach (Communication communication in communications)
            {
                if (tbAll.Text != "")
                    tbAll.Text += "\r\n\r\n" + communication.RecordDateTime.ToString() + "  " + communication.Sender + "：";
                else
                    tbAll.Text +=  communication.RecordDateTime.ToString() + "  " + communication.Sender + "：";

                tbAll.Text += "\r\n" + communication.Content ;
            }

        }

        private void bcOperate_OnButtonClick(object sender, int buttonIndex)
        {
            switch (buttonIndex)
            {
                case 0:  //发送
                    SendFeedback();
                    break;
                case 1:  //键盘
                    InputMethod.SHFullScreen(this.Handle, 4);
                    InputMethod.SipShowIM(1);
                    tbSendContent.Focus();
                    break;
                case 2: //返回
                    FormManage.GotoNext("FrmMain", null, null, true);
                    break;

            }   
        }

        private void SendFeedback()
        {
            if (tbSendContent.Text == "")
                return;

            Communication communication = new Communication();
            communication.CommunicationID = Guid.NewGuid().ToString();
            communication.Content = tbSendContent.Text;
            communication.Direction = "反馈";
            communication.Sender = AppLogic.LoginUser.RealName;
            communication.SenderID = AppLogic.LoginUser.UserName;
            communication.RecordDateTime = DateTime.Now ;

            //先保存反馈信息
            communicationService.AddCommunication(communication);

            //发送反馈信息
            communicationService.SendCommunicationToServer(communication);

            tbSendContent.Text = "";

            if (tbAll.Text != "")
                tbAll.Text += "\r\n\r\n" + communication.RecordDateTime.ToString() + "  " + communication.Sender + "：";
            else
                tbAll.Text += communication.RecordDateTime.ToString() + "  " + communication.Sender + "：";
            tbAll.Text += "\r\n" + communication.Content ;

            GotoLast(tbAll);
        }

        private void FrmCommunication_Activated(object sender, EventArgs e)
        {
            GotoLast(tbAll);
        }

        private void GotoLast(TextBox textBox)
        {
            //让文本框获取焦点 
            textBox.Focus();
            //设置光标的位置到文本尾 
            textBox.Select(tbAll.TextLength, 0);
            //滚动到控件光标处 
            textBox.ScrollToCaret();
        }

        private void GetDatatimer_Tick(object sender, EventArgs e)
        {
            communicationService.DownloadCommunication();
        }

        private void FrmCommunication_Closed(object sender, EventArgs e)
        {
            GetDatatimer.Enabled = false ;
        }

    }
}